/*
	The MIT License

	Copyright (c) 2010 IFMO/GameDev Studio

	Permission is hereby granted, free of charge, to any person obtaining a copy
	of this software and associated documentation files (the "Software"), to deal
	in the Software without restriction, including without limitation the rights
	to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
	copies of the Software, and to permit persons to whom the Software is
	furnished to do so, subject to the following conditions:

	The above copyright notice and this permission notice shall be included in
	all copies or substantial portions of the Software.

	THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
	IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
	FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
	AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
	LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
	OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
	THE SOFTWARE.
*/
// d3d_render_system.h - 


/*-----------------------------------------------------------------------------
	RS :
-----------------------------------------------------------------------------*/

#pragma once



struct  EVidModeDesc_s {
		uint	width;
		uint	height;
		bool	is_supported;
	};


class	ERSMeshSystem;
class	ERSTextureManager;
class	ERSRTManager;


struct  EVidState_s	{
		bool	fullscr;
		int		mode;
		HWND	h_wnd;
	};


class ED3DRenderSystem : public IRenderSystem {
	public:
							ED3DRenderSystem	( void );
		virtual				~ED3DRenderSystem	( void );

		virtual void		InstallForward		( void );
		virtual void		InstallDeferred		( void );
		virtual void		Uninstall			( void );
	
		
		virtual bool		BeginFrame			( void );		
		virtual void		RenderFrame			( void );
		virtual void		EndFrame			( void );
		
		virtual bool		CheckFallback		( uint fallback );
	
		virtual	IPxDraw2D	GetDraw2D			( void ) { return draw2d; }
		
		virtual IPxDSScene	GetDSScene			( void ) { return scene; }
		virtual IPxFRScene	GetFRScene			( void ) { return fr_scene; }
		virtual IPxDVScene	GetDVScene			( void ) { return dbg_scene; }
		
		
		virtual void		GetScreenSize		( uint &w, uint &h );
		virtual void *		GetWndDescriptor	( void );
		virtual uint		GetCurrentMode		( void );
		virtual uint		GetModeNum			( void );
		virtual void		GetModeDesc			( uint nmode, EVidModeDesc_s *mode_desc );

		virtual void		GetTextureSize		( const EString tex_name, uint &w, uint &h );
		
	public:
		bool				RestoreDevice		( void );
		void				InitRSAPI			( void );
	
		ID3DXEffect			*CompileEffect		( const char *path );
		void				DrawFSQuad			( void );
	
		void				ResetDeviceState	( void );
		
		IDirect3DDevice9	*GetDevice			( void ) { return d3ddev; }
		IPxDSScene			GetRScene			( void ) { return scene; }

		void				CheckDeviceFallbacks	( void );
		bool				IsFormatSupported		( D3DFORMAT fmt, D3DRESOURCETYPE resType, DWORD dwUsage );
		
		
	
	public:
		IPxVBManager		mss;
		IPxTXManager		txm;
		IPxRTManager		rtm;
		
	protected:
		static ED3DRenderSystem	*self;
	
		uint		fallback_flags;
	
		IPxDraw2D					draw2d;
	
		LPDIRECT3DSURFACE9		default_color_surf;
		LPDIRECT3DSURFACE9		default_depth_surf;

		LPDIRECT3D9				d3d;
		LPDIRECT3DDEVICE9		d3ddev;
	    D3DPRESENT_PARAMETERS	d3dpp; 
		
		IPxVar			d3d_wireframe;
		
		IPxDSScene		scene;
		IPxFRScene		fr_scene;
		IPxDVScene		dbg_scene;
		
		//
		//	Init/shutdown stuff :
		//
		void			CheckDisplayModes	( void );
		void			InitDisplay			( void );
		void			InitDirect3D		( void );
		void			ShutdownDisplay		( void );
		void			ShutdownDirect3D	( void );

		static void		ListDisplayModes_f	( ED3DRenderSystem *self, int argc, char **argv );
		static LRESULT	WndProc				( HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam );
		EVidState_s		vid_state;		
		IPxVar			d3d_mode;
		IPxVar			d3d_fullscr;
		IPxVar			d3d_xpos;
		IPxVar			d3d_ypos;
		
		IPxVar			d3d_antialiasing;
		
		void			ScreenShot			( void );
		static void		ScreenShot_f		( ED3DRenderSystem *self, int argc, char **argv );
		bool			need_screenshot;
		
	public:
		static int	reload_shaders	( lua_State *L );
	};
